package com.lxj.controller.sys;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.lxj.common.CodeMsg;
import com.lxj.common.Result;
import com.lxj.domain.SysUser;
import com.lxj.dto.SysUserDto;
import com.lxj.service.UserService;
import com.lxj.vo.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author lxj
 * @date 2021/6/16 16:00
 * @Description: 用户的控制器
 */
@RestController
@RequestMapping("/sysuser")
public class SysUserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param username 用户名称
     * @param password 密码
     */
    @PostMapping("/login.do")
    public Result login(String username, String password,String code){
        //校验验证码
        validateCode(code);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        System.out.println("登录成功");
        return new Result();
    }

    /**
     * 获取验证码
     * @param response
     * @param session
     * @throws IOException
     */
    @GetMapping("/codeImg.do")
    public void createCodeImg(HttpServletResponse response, HttpSession session) throws IOException {
        // 1 产生一个验证码 130 × 48
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(130, 48, 1, 3);
        // 2 保持验证码
        session.setAttribute("CODE_IMG", circleCaptcha.getCode());
        // 3 将验证码写出去
        circleCaptcha.write(response.getOutputStream());
    }

    /**
     * 用户的登出
     * @return
     */
    @GetMapping("/logout.do")
    public Result logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new Result();
    }

    /**
     * 新增用户的方法
     * @param dto
     * @return
     */
    @PostMapping("/add.do")
    @RequiresPermissions(value = "user:add")
    public Result addUser(@RequestBody SysUserDto dto){
        return userService.addUser(dto);
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @PostMapping("/delete.do/{id}")
    @RequiresPermissions(value = "user:delete")
    public Result deleteUserById(@PathVariable("id") Integer id){
        return userService.deleteUserById(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete.do/{ids}")
    @RequiresPermissions(value = "user:delete")
    public Result batchDelete(@PathVariable("ids") Integer[] ids){
        return userService.batchDelete(ids);
    }

    /**
     * 修改用户
     * @param dto
     * @return
     */
    @PostMapping("/update.do")
    @RequiresPermissions(value = "user:update")
    public Result updateUser(@RequestBody SysUserDto dto){
        return userService.updateUser(dto);
    }

    /**
     * 修改用户的状态
     * @param dto
     * @return
     */
    @PostMapping("/updateUserStatus.do")
    @RequiresPermissions(value = "user:update")
    public Result updateUserStatus(SysUserDto dto) {
        return userService.updateUser(dto);
    }
    /**
     * 分页查询
     * @param dto
     * @return
     */
    @GetMapping("/findByPage.do")
    public Result selectPage(SysUserDto dto){
        PageInfo<SysUser> pageInfo = userService.selectPage(dto);
        return new Result(pageInfo);
    }

    /**
     * 校验验证码
     * @param code
     */
    private void validateCode(String code) {
        //获取session 里面的验证码
        ServletRequestAttributes req = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = req.getRequest().getSession();
        Object codeImg = session.getAttribute("CODE_IMG");
        //对比两个验证码
        if(!codeImg.toString().equalsIgnoreCase(code)){
            throw new IllegalArgumentException(CodeMsg.USER_CODE_ERROR.msg);
        }
    }
}
